package com.eascs.archive.controller.credit;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.admin.o2o.internal.util.BaseUtils;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.admin.o2o.o2ozx.dao.O2oZxDao;
import com.eascs.admin.o2o.zx.entity.O2oLoanUserEntityZx;
import com.eascs.admin.o2o.zx.entity.O2oZxEntity;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.archive.entity.CreditArchiveRecordEntity;
import com.eascs.archive.form.ArchivequeryForm;
import com.eascs.archive.service.ArchiveRecordService;
import com.eascs.archive.service.ArchiveService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.tag.MenuTag;
import com.eascs.web.url.UrlHelper;
import com.eascs.workflow.common.entity.wf.ProcessTemplate;

@Controller
public class ArchiveQueryRestController extends BaseRestController {
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private O2oLoanapplyService o2oloanapplyService;
	@Autowired
	private ArchiveRecordService archiveRecordService;
	@Autowired
	private BaseUtils baseUtils;
	@Autowired
	ArchiveService archiveService;
	@Autowired
	O2oZxDao o2oZxDao;

	@Override
	public void preHandle() {
		super.preHandle();
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "afterLoanManage");
	}

	@RequestMapping("/archive/attachmentQuery")
	public HtmlRestResult archiveCreditDetailQuery(ArchivequeryForm entity) {
		CreditArchiveRecordEntity vo = archiveRecordService.getById(entity.getId());
		ViewContext.put("vo", vo);
		O2oZxEntity zxEntity = o2oZxDao.getById(O2oZxEntity.class, vo.getLoanapplyNo());
		if(zxEntity!=null){
			ViewContext.put("zxEntity", zxEntity);
			O2oLoanUserEntityZx zxUserEntity = o2oZxDao.getLoanUserZx(zxEntity.getId());
			ViewContext.put("zxUserEntity", zxUserEntity);
		}

		//O2oLoanapplyEntity v1 = o2oloanapplyService.findBillCode1(vo.getBillCode());
		//ViewContext.put("loanapply", v1);
		attachmentQuery(entity.getId());
		String agreeUrl = baseUtils.getApprovalAgreeUrl(entity.getId(), null, ProcessTemplate.SYSTEM_MODULE_O2O);
		ViewContext.put("agreeUrl", agreeUrl);
		return HtmlRestResult.popHtmlFragment("archive/credit/archive_datail_view");
	}
	
	/**
	 * @Description:
	 *               <p>
	 *               脱敏查看弹窗
	 *               </p>
	 * @author LMX
	 */
	@RequestMapping("/archive/show_window")
	public BaseRestResult<String> ShowWindow(String id) {
		CreditArchiveRecordEntity vo = archiveRecordService.getById(id);
		ViewContext.put("vo", vo);
		O2oZxEntity zxEntity = o2oZxDao.getById(O2oZxEntity.class, vo.getLoanapplyNo());
		if(zxEntity!=null){
			ViewContext.put("zxEntity", zxEntity);
			O2oLoanUserEntityZx zxUserEntity = o2oZxDao.getLoanUserZx(zxEntity.getId());
			ViewContext.put("zxUserEntity", zxUserEntity);
		}
		return HtmlRestResult.popHtmlFragment("archive/credit/archive_datail_sensitive");
	}

	public void attachmentQuery(String id) {
		PageResult<Object[]> pageResult = o2oloanapplyService.findO2oDataListEntityById(id);
		String titles[] = { "附件类型", "文件名", "提交人", "提交时间", "更新时间" };
		Button[] titleButtons = new Button[] { null,
				ButtonHelper.createOpenButton(null, "/file/download?datalistno=") };
		PaginationInfo datalistInfo = new PaginationInfo(pageResult, titles, "/loanUser/loanudpate");
		datalistInfo.setTitleButtons(titleButtons);
		datalistInfo.setBuildPagingNavigation(false);
		datalistInfo.setFirstRenderColumnIndex(1);
		datalistInfo.setBuildRowNumber(true);
		ViewContext.put("datalistInfo", datalistInfo);
	}

	@RequestMapping("/archive/startUpFile")
	public BaseRestResult<String> startUpFile(String id) {
		try {
			archiveService.handStartUp(id);
		} catch (Exception e) {
			logger.info("流程启动失败：[{}]", e.getMessage());
			return BaseRestResult.error(e.getMessage());
		}
		return BaseRestResult.info("启动流程成功").andRedirect(UrlHelper.build("/archive/query"));
	}
}
